package com.training1.core.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.LoginService;
import com.training1.core.modules.service.MemberService;

@Controller
@RequestMapping("member")
public class MemberController extends BaseController{
	
	@Resource
	private MemberService memberService;
	
	@Resource
	private LoginService loginService;
	
	
	/**
	 * 进入会员列表页面
	 * @return
	 */
	@RequestMapping(value={"","/"})
	public String go(){
		return "member/member";
	}
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	@ResponseBody
	public Page<MemberEntity> list(Integer userTrust,String blackListFlag,String branchId,String memberName,String cellPhone, Page<MemberEntity> page){
		if (LoginInfoUtil.getLoginInfo().getLoginUser().getUserRole()==1) {
			branchId = LoginInfoUtil.getLoginInfo().getLoginUser().getUserAccount(); 
		};
		/**
		 * 这里当选择"全部"的branchId在设置值时为"12345"
		 */
		page.getSearch().put("branchId", "12345".equals(branchId)?null:branchId);
		//page.getSearch().put("branchId", branchId);
		page.getSearch().put("userTrust", userTrust);
		page.getSearch().put("memberName",memberName);
		page.getSearch().put("cellPhone",cellPhone);
		page.getSearch().put("blackListFlag",blackListFlag);
		memberService.findList(page.getSearch());
		return page;
	}
	
	/**
	 * 查询开单中心名称
	 * @param f
	 * @return
	 */
	@RequestMapping(value="findUserName",method=RequestMethod.GET)
	@ResponseBody
	public List<UserEntity> find(boolean f){
		List<UserEntity> userItems = new ArrayList<UserEntity>();
		if (f) {
			UserEntity user = new UserEntity();
			user.setUsername("全部");
			user.setUserAccount("12345");
			userItems.add(user);
		};
		Map<String, Object> param = new HashMap<>();
		param.put("userRole", 1);
		List<UserEntity> items = memberService.findUserName(param);
		userItems.addAll(items);
		return userItems;
	}
	
	/**
	 * 根据推荐人手机号查询姓名
	 * @param recommenderCellphone
	 * @param user
	 * @return
	 */
	@RequestMapping(value="findNameByCellphone",method=RequestMethod.GET)
	@ResponseBody
	public MemberEntity find(String recommenderCellphone){
		MemberEntity member = memberService.findMemberByRecommender(recommenderCellphone);
		return member;
	}
	
	//移入或者移除黑名单
	@RequestMapping(value="findMemberListById",method={RequestMethod.GET})
	@ResponseBody
	public RespJson findMemberListById(String allId,Integer blackFlag,Page<MemberEntity> page){
		page.getSearch().put("allId", allId);
		page.getSearch().put("blackFlag", blackFlag);
		memberService.findMemberListById(page.getSearch());
		return RespJsonFactory.buildSuccess();
	}
	
	//托管状态设置
	@RequestMapping(value="updateMemberTrust",method={RequestMethod.POST})
	@ResponseBody
	public RespJson updateMemberTrust(String allId,Integer flag,Page<MemberEntity> page){
		page.getSearch().put("allId", allId);
		page.getSearch().put("flag", flag);
		memberService.updateMemberTrust(page.getSearch());
		return RespJsonFactory.buildSuccess();
	}
	//编辑/查看
	@RequestMapping(value="get")
	@ResponseBody
	public MemberEntity get(Integer id){
		return memberService.get(id);
	}
	@RequestMapping(value="gett")
	@ResponseBody
	public MemberEntity gett(Integer id){
		return memberService.gett(id);
	}
	//账户调整
	//保存,账户调整的
	@RequestMapping(value="save")
	@ResponseBody
	public RespJson save(MemberEntity memberEntity,Integer acc){
		if(acc == 1){//账户调整的
			memberService.updateAcc(memberEntity);
		}else{
			memberService.save(memberEntity);
		}
		return RespJsonFactory.buildSuccess();
	}
	
	
	//goMemberInfo查看会员详情页面跳转
	@RequestMapping(value="goMemberInfo")
	public String goMemberInfo(Integer id,Model model){
		MemberEntity memberEntity = memberService.get(id);
		model.addAttribute(memberEntity);
//		model.addAttribute(memberEntity);
		return "member/memberInfo";
	}
	
	//goMemberInfo查看会员详情页面跳转
	@RequestMapping(value="memberEdit")
	@ResponseBody
	public MemberEntity memberEdit(Integer id){
		MemberEntity memberEntity = memberService.get(id);
		return memberEntity;
	}
	
	
	//goMemberAdd添加会员详情页面跳转
	@RequestMapping(value="goMemberAdd")
	public String goMemberAdd(String id,Model model){
		//id不为空是编辑,为空是添加会员信息
		if(id != null){
			model.addAttribute("id", id);
			return "member/memberEdit";
		}
		return "member/memberAdd";
	}
	
	
	//验证会员手机号是否已存在
	@RequestMapping(value="findMemberResult")
	@ResponseBody
	public String isExist(String cellPhone){
		MemberEntity memberEntity = new MemberEntity();
		memberEntity = memberService.findMember(cellPhone);
		if(memberEntity != null)
			return "1";
		return null;
	}
	
	//添加的表单提交//前台的验证功能还未提交
	@RequestMapping(value="create")
	public String create(MemberEntity memberEntity){
		memberService.save(memberEntity);
		return "member/memberAdd";
	}
	
	
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp,String blackListFlag, String cellphone,String memberName,String branchId,String userTrust){
		Map<String, Object> param = new HashMap<String, Object>();
		/*if(StringUtils.anyIsEmpty(blackListFlag) ||  ("Mark").equals(blackListFlag)){//下拉框不选或选“全部”
			blackListFlag = null;
		}
		if(StringUtils.anyIsEmpty(branchId) ||  ("12345").equals(branchId)){//下拉框不选或选“全部”
			branchId = null;
		}*/
		param.put("memberName", null);
		param.put("cellPhone", null);
		param.put("branchId", null);
		param.put("blackListFlag",null);
		param.put("userTrust",null);
		memberService.export(resp, param);
	}
}
